<template>
  <div class="app-container">
    <div class="div-t">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增禁止通行行程码</el-button>
        </el-col>
        <right-toolbar :search="false" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        ref="tables"
        border
        v-loading="loading"
        :data="list"
        @selection-change="handleSelectionChange"
        :default-sort="defaultSort"
        @sort-change="handleSortChange"
      >
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column prop="deptName" width="100" label="所属公司" align="center" />
        <el-table-column label="禁止通行行程码" align="center" prop="tripCode">
          <template slot-scope="scope">
            <el-tag
              v-for="item in setTag(scope.row)"
              :key="item.index"
              size="mini"
              type="danger"
            >{{item}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="启用 / 禁用" align="center" prop="isDisable">
          <template slot-scope="scope">
            <el-switch
              @change="disableThis($event,scope.row.id)"
              v-model="scope.row.isDisable"
              active-color="#ff4949"
              inactive-color="#13ce66"
              active-text="禁用"
              inactive-text="启用"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleUpdate(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color:red"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改标签的对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
          <el-form-item label="禁止通行行程码" prop="tripCode">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.tripCode"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addTag, updateTag, batchDeleteTag } from "@/api/business/tag";
import { tripApi } from "@/api/epidemic/tripCode";

export default {
  name: "tripCode",
  dicts: ["sys_oper_type", "sys_common_status"],
  data() {
    return {
      tableHeight: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中标签id数组
      tagIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "operTime", order: "descending" },
      // 表单参数
      // 弹出层标题
      title: "",
      form: {
        tripCode: ""
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isDelete: false
      },
      // 表单校验
      rules: {
        healthCode: [
          { required: true, message: "请选择禁止通行健康码", trigger: "blur" }
        ],
        tripCode: [
          { required: true, message: "请选择禁止通行行程码", trigger: "blur" }
        ],
        nucleicAcid: [
          { required: true, message: "请选择核酸有效期", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    disableThis(value, id) {
      this.loading = true;
      tripApi
        .tripDisable(id, value)
        .then(res => {
          this.loading = false;
          this.getList();
          this.$message({
            message: value == true ? "禁用成功" : "启用成功",
            type: "success"
          });
        })
        .catch(err => {
          this.loading = false;
        });
    },
    setTag(row) {
      if (row.tripCode != null) {
        const tripCode = JSON.parse(row.tripCode);
        return tripCode;
      } else {
        return [];
      }
    },
    /** 查询 */
    getList() {
      this.loading = true;
      tripApi
        .tripList(this.queryParams)
        .then(res => {
          if (res.data.records) {
            this.list = res.data.records;
          } else {
            this.list = [];
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
        });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加禁止通行行程码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改禁止通行行程码";
      this.form.tripCode = JSON.parse(row.tripCode).toString();
      this.form.id = row.id;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm("是否确认删除？", "提示").then(res => {
        tripApi.tripDelete(row.id).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      });
    },
    // 表单重置
    reset() {
      this.form = {
        tripCode: ""
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tripCode = this.form.tripCode.replace("，", ","); // 中文逗号替换英文逗号
          this.form.tripCode = this.form.tripCode.split(","); // 转换为数组
          if (this.form.id != undefined) {
            tripApi.tripUpdate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            tripApi.tripAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
<style scoped>
.el-tag {
  margin-right: 10px;
}
.div-t {
  background: white;
  padding: 10px 10px 20px 10px;
  border-radius: 5px;
  /* border: 1px solid rgb(211, 208, 208); */
}
.div-sch {
  background: white;
  padding: 10px 10px 0px 10px;
  border-radius: 5px;
  margin-bottom: 10px;
}
</style>